package com.kevin.shop.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.kevin.shop.domain.entity.ShopGoodSelection;
import com.kevin.shop.mapper.ShopGoodSelectionMapper;
import com.kevin.shop.service.IShopGoodSelectionService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ExceptionUtil;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIBuilder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 选品Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-16
 */
@Service
public class ShopGoodSelectionServiceImpl implements IShopGoodSelectionService 
{
    @Resource
    private ShopGoodSelectionMapper shopGoodSelectionMapper;

    /**
     * 查询选品
     * 
     * @param selectionId 选品主键
     * @return 选品
     */
    @Override
    public ShopGoodSelection selectShopGoodSelectionBySelectionId(Long selectionId)
    {
        return shopGoodSelectionMapper.selectShopGoodSelectionBySelectionId(selectionId);
    }

    /**
     * 查询选品列表
     * 
     * @param shopGoodSelection 选品
     * @return 选品
     */
    @Override
    public List<ShopGoodSelection> selectShopGoodSelectionList(ShopGoodSelection shopGoodSelection)
    {
        return shopGoodSelectionMapper.selectShopGoodSelectionList(shopGoodSelection);
    }

    /**
     * 新增选品
     * 
     * @param shopGoodSelection 选品
     * @return 结果
     */
    @Override
    public int insertShopGoodSelection(ShopGoodSelection shopGoodSelection)
    {
        shopGoodSelection.setCreateTime(DateUtils.getNowDate());
        return shopGoodSelectionMapper.insertShopGoodSelection(shopGoodSelection);
    }

    @SneakyThrows
    @Override
    public int addBatch(Set<String> goods) {
        Set<String> ids = CollectionUtil.newHashSet();
        for (String good : goods) {
            if(good.startsWith("http")) {
                good = good.replace("http", "");
                URIBuilder uriBuilder = new URIBuilder(good);
                Map<String, String> params = uriBuilder.getQueryParams().stream()
                        .collect(Collectors.toMap(
                                NameValuePair::getName,
                                NameValuePair::getValue
                        ));
                if(StrUtil.isBlank(params.get("id"))){
                    ExceptionUtil.throwServiceException("链接未包含id参数,", good);
                }
                good = params.get("id");
            }
            ids.add(good);
        }

        List<ShopGoodSelection> shopGoodSelections =ids.stream().map(id-> new ShopGoodSelection().setGoodId(id)).toList();
        shopGoodSelections.forEach(shopGoodSelection-> shopGoodSelectionMapper.insertShopGoodSelection(shopGoodSelection));

        return goods.size();

    }

    /**
     * 修改选品
     * 
     * @param shopGoodSelection 选品
     * @return 结果
     */
    @Override
    public int updateShopGoodSelection(ShopGoodSelection shopGoodSelection)
    {
        shopGoodSelection.setUpdateTime(DateUtils.getNowDate());
        return shopGoodSelectionMapper.updateShopGoodSelection(shopGoodSelection);
    }

    /**
     * 批量删除选品
     * 
     * @param selectionIds 需要删除的选品主键
     * @return 结果
     */
    @Override
    public int deleteShopGoodSelectionBySelectionIds(Long[] selectionIds)
    {
        return shopGoodSelectionMapper.deleteShopGoodSelectionBySelectionIds(selectionIds);
    }

    /**
     * 删除选品信息
     * 
     * @param selectionId 选品主键
     * @return 结果
     */
    @Override
    public int deleteShopGoodSelectionBySelectionId(Long selectionId)
    {
        return shopGoodSelectionMapper.deleteShopGoodSelectionBySelectionId(selectionId);
    }
}
